//获取应用实例
const app = getApp()
// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    des: [],
    pushOver: false,
    list: [],
    offset: 0,
    num:20,
    desData:{},
    wineid:'',
    id:"",
    target:'',
    type:"",
    more:'',
    activeTab: 0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'传参分享')
    let that=this
    that.setData({
      id:options.id,
      wineid: options.brandid

    })
    console.log(options)
    if(options.id=='1'){
      let that=this
      this.setData({
        name: options.brandid
      })
      app.request({
        url: '/WhiskyBrandPageList',
        data:{
          brandid:options.brandid
        },
        method:'post',
        success: function (res) {
          that.setData({
            des:res.data.brandList,
            target: res.data.brandList.name,
          })
         
          app.request({ 
            url: '/selectBrandVo',
            data:{
              name:res.data.brandList.name,
              offset:0,
              num:20
            },
            method:'post',
            success: function (res) {
              that.setData({
                list:res.data.records,
                type:'brand',
                more:res.data.more
              })
            }
          })
        }
      })

     
    } 
    if(options.id=='2'){
      let that=this
      this.setData({
        name: options.brandid,
       
      })
      app.request({
        url: '/WhiskyDistilleryListPageList',
        data:{
          distilleryid: options.brandid
        },
        method:'post',
        success: function (res) {
          console.log(res.data.distilleryid,"es.data")
          that.setData({
            des:res.data.DistilleryList,
            target: res.data.DistilleryList.name,
            more: res.data.more
          })
          app.request({
            url: '/selectDistillery',
            data:{
            distillery:res.data.DistilleryList.name,
            offset:0,
            num:20
            },
            method:'post',
            success: function (res) {
             
              that.setData({
                list:res.data.records,
                type: 'distillery',
                more: res.data.more
              })
            }
          })
          // that.setData({ list: that.data.list.concat(res.data.list), offset: that.data.offset + 1 })
        }
      })
      
    } 
    if(options.id=='3'){
      let that=this
      this.setData({
        name: options.brandid,
        
        
      })
      app.request({
        url: '/WhiskyBusinessPageList',
        data:{
          businessid:options.brandid,
        },
        method:'post',
        success: function (res) {
          console.log(res.data)
          that.setData({
            des:res.data.BusinessList,
            target: res.data.BusinessList.name,
          })
          app.request({
            url: '/selectBusiness',
            data:{
              name:res.data.BusinessList.name,
            offset:0,
            num:20
            },
            method:'post',
            success: function (res) {
             
              that.setData({
                list:res.data.records,
                type:'business',
                more: res.data.more
              })
            }
          })
          // that.setData({ list: that.data.list.concat(res.data.list), offset: that.data.offset + 1 })
        }
      })
     
    } 
    else{
      let that = this
      let typeArrs = ['威士忌详情', '品牌详情', '酒厂详情', '装瓶商详情']
     
      // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
      // eventChannel.on('acceptDataFromOpenerPage', function (data) {
      var goodsNm = decodeURI(options.id)
      console.log(goodsNm,'解码')
      var data = JSON.parse(options.id)
        wx.setNavigationBarTitle({ title: typeArrs[data.typeId] });
      
        that.getDes(data)
        that.getData(data)
      // })
      that.setData({
        wineid: options.id
      })
    }
  },
  getDes(data){
    console.log(data)
    let that = this
    let typeArr = ['whisky','brand','distillery','business']
    let dataZ = [
      {
        wineid: data.wineid
      },
      {
        brandid: data.brandid
      },
      {
        distilleryid: data.distilleryid
      },
      {
        businessid: data.businessid
      }
    ]
    let datas = dataZ[data.typeId]
    app.request({
      url: '/' + typeArr[data.typeId] + '/get-' + typeArr[data.typeId],
      data: datas,
      success: function(res) {
        console.log(res.data)
        that.setData({des: res.data,activeTab: data.typeId})
      }
    })
  },
  getData(data){
    let that = this
    that.data.desData = data
    if(that.data.pushOver) return
    let typeArr = ['whisky','brand','distillery','business']
    let datas = {
      target: data.name,
      type: typeArr[data.typeId],
      offset: that.data.offset,
      num: 20
    }
    
    app.request({
      url: '/whisky/composite-list',
      data: datas,
      success: function(res) {
        console.log(res.data.list)
        if(res && res.data && !res.data.list)return
        if(that.data.offset > res.data.total_page)
        {
          that.setData({pushOver:true})
          return
        }
        that.setData({list: that.data.list.concat(res.data.list), offset: that.data.offset + 1})
      }
    })
  },
  goDes(e){
   
    let data = e.currentTarget.dataset.item
    data.typeId = this.data.activeTab
    wx.navigateTo({
      url: '/pages/works/index?id=' + e.currentTarget.dataset.item.wineid,
     
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({
    //   datas:wx.getStorageSync('data')
    // })
    console.log(this.data.wineid)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(1212)
   if(this.data.more==true){
     let that = this
     // onsole.log('触底')
     app.request({
       url: '/selectDistillery',
      data:{
        distillery:this.data.target,
        num:20,
        offset:this.data.offset
      },
      method:'post',
       success: function (res) {
         that.setData({
           list: that.data.list.concat(res.data.records),
           offset: res.data.offset,
           more:res.data.more,
         })
       }
     })
   }
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    path: '/pages/details/index?id=' + this.data.id + '&brandid=' + this.data.wineid
  }

})